﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Base;
using ZBJF.Warranty.Common.Enums.InfoCollect;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductAuditProjectConfigurationSnapshootService
    {
        /// <summary>
        /// 创建审核配置快照
        /// </summary>
        /// <param name="insuranceOrderId">保单表InsuranceOrder主键ID</param>
        /// <param name="serviceProjectType">保险信息类型：和ServiceProductType是同一个枚举，根据ServiceProjectType去不同的表取数据，比如手机维修保险信息，就去CellPhoneInsuranceInfo表取</param>
        /// <returns></returns>
        public bool CreateSnapshoot(long insuranceOrderId, ServiceProductType serviceProjectType)
        {
            var result = true;
            var cellPhoneInsuranceInfoList = ServiceFactory.CellPhoneInsuranceInfoServiceInstance.LoadByInsuranceID(insuranceOrderId);//todo 根据ServiceProjectType去不同的表取数据
            if (cellPhoneInsuranceInfoList.Any())
            {
                var cellPhoneServiceProjectIDs =
                    cellPhoneInsuranceInfoList.Select(o => o.CellPhoneServiceProjectID.Value).ToList();
                var auditProjectConfigurationList =
                    ServiceFactory.InsuranceProductsAuditProjectConfigurationMappingsServiceInstance.GetAuditProjectConfigurationList(cellPhoneServiceProjectIDs, serviceProjectType).ToList();
                if (auditProjectConfigurationList.Any())
                {
                    for (int i = 0; i < auditProjectConfigurationList.Count; i++)
                    {
                        if (!result)
                            return false;
                        var auditProjectConfiguration = auditProjectConfigurationList[i];
                        var entity = new InsuranceProductAuditProjectConfigurationSnapshoot
                        {
                            InsuranceOrderServiceProjectId = auditProjectConfiguration.InsuranceOrderServiceProjectId,
                            ServiceProjectType = (byte)serviceProjectType,
                            AuditProjectWithType = auditProjectConfiguration.AuditProjectWithType,
                            AuditProjectID = auditProjectConfiguration.AuditProjectID,
                            AuditProjectName = auditProjectConfiguration.AuditProjectName,
                            FieldType = auditProjectConfiguration.FieldType,
                            OptionName = auditProjectConfiguration.OptionName,
                        };
                        result = AddEntities(entity).ID > 0;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 获取审核配置
        /// </summary>
        /// <param name="insuranceOrderServiceProjectId">选择的是哪个服务项目</param>
        /// <param name="serviceProjectType">保险信息类型：和ServiceProductType是同一个枚举，根据ServiceProjectType去不同的表取数据，比如手机维修保险信息，就去CellPhoneInsuranceInfo表取</param>
        /// <param name="auditProjectWithType"></param>
        /// <returns></returns>
        public IQueryable<InsuranceProductAuditProjectConfigurationSnapshoot> GetAuditProjectConfigurationList(long insuranceOrderServiceProjectId, ServiceProductType serviceProjectType, byte auditProjectWithType = 0)
        {
            var byteServiceProductType = (byte)serviceProjectType;
            var predicate = PredicateExtensions.True<InsuranceProductAuditProjectConfigurationSnapshoot>().And(o => o.InsuranceOrderServiceProjectId == insuranceOrderServiceProjectId && o.ServiceProjectType == byteServiceProductType);
            if (auditProjectWithType > 0)
                predicate = predicate.And(o => o.AuditProjectWithType == auditProjectWithType);

            return LoadEntities(predicate);
        }
    }
}